/**
 @Name: 地区选择-前台页面
 @Date: 2014.10.29
 * usage:
* $.SELAREA({provinceId:'province',cityId:'city',districtId:'district',areacode:'101101101101'});
*pubsub：
  $('#province').parent().bind('areaChange',function(){});
*/

;(function (global, $, undefined) {
	"use strict";
	if (!$) {
		throw "AreaPiker requires jQuery"; // jQuery is not loaded.
	}
	if ($ && $.SELAREA) { return; } // SELAREA is already loaded

	$.SELAREA = function (options) {
		var defaults = {
				provinceId:"province",
				cityId:"city",
				districtId:"district",
				areacode:"",
                provinceFirst: '全国/省区/直辖市',   //省级选择框默认显示文字
                cityFirst : '全部/市',             //市级选择框默认显示文字
                districtFirst : '全部/县/区',       //县区级选择框默认显示文字
                rmCityNo1 : false,                //是否生成市级选择框第一项
                includeCountry: true,              //省级选择框是否包含全国选项
                allowSelectAllDistrict: false     //允许选择districtFirst
        };
		var options = $.extend(defaults,options);
		var province = $("#"+options.provinceId), city = $("#"+options.cityId), district = $("#"+options.districtId), countryCode = options.areacode;
		var PROVINCE_FIRST_OPTION = options.provinceFirst, CITY_FIRST_OPTION = options.cityFirst, DISTRICT_FIRST_OPTION = options.districtFirst;
		    
			province.find('div:first').html(PROVINCE_FIRST_OPTION).bind('click mouseover',function(){
				 //var state= province.find('div:last').css('display');
				 //state=='none'?province.find('div:last').show():province.find('div:last').hide();
				 province.find('div:last').show();
				 city.find('div:last').hide();
				 district.find('div:last').hide();
			});
			city.find('div:first').html(CITY_FIRST_OPTION).bind('click mouseover',function(){
				 //var state= city.find('div:last').css('display');
				 //state=='none'?city.find('div:last').show():city.find('div:last').hide();
				 city.find('div:last').show();
				 province.find('div:last').hide();
				 district.find('div:last').hide();
			});
			district.find('div:first').html(DISTRICT_FIRST_OPTION).bind('click mouseover',function(){
				 //var state= district.find('div:last').css('display');
				 //state=='none'?district.find('div:last').show():district.find('div:last').hide();
				 district.find('div:last').show();
				 province.find('div:last').hide();
				 city.find('div:last').hide();
			});
	    
		 province.find('div:last').on('click','a',function () {province.data('val',$(this).data('val')).find("div:first").html($(this).html()).show();province.find('div:last').hide();$(this).trigger("areaChange",[province.attr('id')]);city.find('div:last').show();return false;});
		city.find('div:last').on('click','a',function () {city.data('val',$(this).data('val')).find("div:first").html($(this).html()); city.find('div:last').hide();$(this).trigger("areaChange",[city.attr('id')]);district.find('div:last').show();return false;});
		district.find('div:last').on('click','a',function () {district.data('val',$(this).data('val')).find("div:first").html($(this).html()); district.find('div:last').hide();$(this).trigger("areaChange",[district.attr('id')]);return false;});
        
		province.parent().bind("areaChange",area_change);

		function area_change(event,id,c){
				var $this = $("#"+id),v=$this.data('val'),$cld = null;
				if (id===0&&c!==undefined)
				{
                    v = 0;
					$cld=province;
                    if(options.includeCountry){
                        province.data('val','').show().find("div:first").html(PROVINCE_FIRST_OPTION);
                        province.find('div:last').html('<a href="javascript:void(0);" data-val="1">全国</a>').hide();
                    }

                    if(options.rmCityNo1) {
                        city.hide();
                    }else{
                        city.data('val','').hide().find("div:first").html(CITY_FIRST_OPTION);
                        city.find('div:last').html('<a href="javascript:void(0);" data-val="">'+CITY_FIRST_OPTION+'</a>');
                    }

					district.data('val','').hide().find("div:first").html(DISTRICT_FIRST_OPTION);
					district.find('div:last').html('<a href="javascript:void(0);" data-val="">'+DISTRICT_FIRST_OPTION+'</a>');
				} else if (id==province.attr('id'))
				{
					$cld =city;
					
					if (v==1||v=='') {
						city.data('val','').hide();
						district.data('val','').hide();
						return ;
					}
                    if(options.rmCityNo1) {
                        city.data('val','').show().find("div:first").html(CITY_FIRST_OPTION);
                        city.find('div:last').html('').hide();
                    }else{
                        city.data('val','').show().find("div:first").html(CITY_FIRST_OPTION);
                        city.find('div:last').html('<a href="javascript:void(0);" data-val="">'+CITY_FIRST_OPTION+'</a>').hide();
                    }

					district.data('val','').hide().find("div:first").html(DISTRICT_FIRST_OPTION);
					district.find('div:last').html('<a href="javascript:void(0);" data-val="">'+DISTRICT_FIRST_OPTION+'</a>');
				} else if (id==city.attr('id'))
				{
                    $cld=district;
					if (v=='') {
						district.data('val','').hide();
						return ;
					}

                    if(options.allowSelectAllDistrict) {
                        district.data('val',v).show().find("div:first").html(DISTRICT_FIRST_OPTION);
                        district.find('div:last').html('<a href="javascript:void(0);" data-val="'+v+'">'+DISTRICT_FIRST_OPTION+'</a>');
                    }else{
                        district.data('val','').show().find("div:first").html(DISTRICT_FIRST_OPTION);
                        district.find('div:last').html('<a href="javascript:void(0);" data-val="">'+DISTRICT_FIRST_OPTION+'</a>');
                    }

				} else if (id==district.attr('id')) {

                    district.data('val',v).find("div:first").html(district.find("[data-val='"+v+"']").html());
				   district.find('div:last').hide(); 
				}
				if ($cld!==null&&v!=='')
				{
					$.getJSON("http://www.net114.com/ajax/get_area_data-id-"+v+".html?jsoncallback=?", function(data){
						  if (data==='error') throw countryCode+" is not a areacode!";
						  var str = '';
						  $.each(data, function(i,item){
								if (item!='') str +=   '<a href="javascript:void(0);" data-val="'+i+'">'+item+'</a>';
						  });
						  if (str!=='') $cld.find('div:last').append(str).end().find("div:first").show();
						  if (c!==undefined && str!=='')
						  {
							  var L=0;
			
					         if ($cld.attr('id')==province.attr('id')&&c.length>=6||c==1)
					          {
								  
								 L=6;
					          } else if ($cld.attr('id')==city.attr('id')&&c.length>=9)
					          {
								  L=9;
					          } else if ($cld.attr('id')==district.attr('id')&&c.length>=12)
					          {
								  L=12;
					          }
							  if (L!==0)  { $cld.data('val',c.slice(0,L)).find("div:first").text($cld.find("[data-val='"+c.slice(0,L)+"']").text()); $cld.trigger("areaChange",[$cld.attr('id'), c]); }
						  } else if (c===undefined&&$cld.attr('id')==city.attr('id')&&$cld.find('a').size()==2)
						  {
							      var la=$cld.find('a:last');
							      $cld.data('val',la.data('val')).find("div:first").html(la.html());
								  $cld.trigger("areaChange",[$cld.attr('id')]);
						  }
					});
				}

			return false;
		}
		area_change(null,0,countryCode);
	};
})(this, this.jQuery);
